//
//  NSString+InputCheck.m
//  EducationAdminClient
//
//  Created by KWSD on 15/12/23.
//  Copyright © 2015年 周佳兴. All rights reserved.
//

#import "NSString+InputCheck.h"

@implementation NSString (InputCheck)


+ (BOOL)witchRoleis:(NSString *)str withStr:(NSString *)string{
    if([str isEqualToString:@"6"]){
      return string.validateMobile;
    }else if ([str isEqualToString:@"4"]){
        return string.validateEmail;
    }else if ([str isEqualToString:@"9"]){
        return string.validateUsername;
    }
    else if ([str isEqualToString:@"3"]){
        return [string validateIdentityCard:string];
    }
    return YES;
}

- (BOOL) validateEmail

{
    
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A- -z0-9.-]+\\.[A-Za-z]{2,4}";
    
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    
    return [emailTest evaluateWithObject:self];
    
}

+ (BOOL) validateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

// ^[0-9]+$

- (BOOL) validateMobile
{
    
    NSString *phoneRegex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
    
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    
    return [phoneTest evaluateWithObject:self];
    
}


- (BOOL) validateUserId

{
    
    NSString *userIdRegex = @"^[0-9]+$";
    
    NSPredicate *userIdPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userIdRegex];
    
    BOOL B = [userIdPredicate evaluateWithObject:self];
    
    if (![self hasPrefix:@"1"] && B) {
        
        return NO;
        
    }
    
    return B;
    
}


- (BOOL) validateUsername

{
    
    NSString *userIdRegex = @"[^,.!@?#$%\\^&*()]+";
    
    NSPredicate *userIdPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userIdRegex];
    
    BOOL B = [userIdPredicate evaluateWithObject:self];
    
//    if (![self hasPrefix:@"1"] && B) {
//        
//        return NO;
//        
//    }
    
    return B;
    
}



//身份证号
- (BOOL) validateIdentityCard: (NSString *)identityCard
{
    BOOL flag;
    if (identityCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:identityCard];
}


@end
